/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eleric.rememome.db.impl;

import com.eleric.rememome.db.AbstractDbRetriever;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.eleric.rememome.db.DbRetriever;
import com.eleric.rememome.db.factory.impl.MemoDbRetrieverFactory;
import com.eleric.rememome.memo.Memo;
import com.eleric.rememome.memo.MemoCategory;
import com.eleric.rememome.memo.impl.MemoVer2;
import com.eleric.rememome.misc.FieldAndValue;
import com.eleric.rememome.misc.FieldAndValueOperator;
import com.eleric.rememome.type.MemoStatusType;
import com.eleric.rememome.type.OrderType;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

/**
 *
 * @author MWaldman
 */
public class DbMemoRetrieverVer2 extends AbstractDbRetriever<Memo>
{

//    public static final String KEY_ROWID = "_id";
//    public static final String KEY_MEMO = "memo";
//    public static final String KEY_CAT = "category";
//    public static final String KEY_STATUS = "status";
//    public static final String KEY_CREATION_TIME = "creationTimestamp";
//    public static final String KEY_STATUS_TIME = "statusTimestamp";
//    public static final String KEY_PRIORITY = "priority";
//    public static final String[] COLUMNS =
//    {
//        KEY_ROWID,
//        KEY_MEMO,
//        KEY_CAT,
//        KEY_STATUS,
//        KEY_CREATION_TIME,
//        KEY_STATUS_TIME,
//        KEY_PRIORITY
//    };
    public static final String KEY_ROWID = MemoDbRetrieverFactory.KEY_ROWID;
    public static final String KEY_MEMO = MemoDbRetrieverFactory.KEY_MEMO;
    public static final String KEY_CAT = MemoDbRetrieverFactory.KEY_CAT;
    public static final String KEY_STATUS = MemoDbRetrieverFactory.KEY_STATUS;
    public static final String KEY_CREATION_TIME = MemoDbRetrieverFactory.KEY_CREATION_TIME;
    public static final String KEY_STATUS_TIME = MemoDbRetrieverFactory.KEY_STATUS_TIME;
    public static final String KEY_PRIORITY = MemoDbRetrieverFactory.KEY_PRIORITY;
    public static final String[] COLUMNS = MemoDbRetrieverFactory.COLUMNS;

    private DbRetriever<MemoCategory> catRetriever;
    private static final int DATABASE_VERSION = 2;
    private final String DATABASE_CREATE_MEMOS =
            "create table " + getTable() + " " +
            "(" + KEY_ROWID + " integer primary key autoincrement, " +
            KEY_MEMO + " text not null, " +
            KEY_CAT + " integer not null, " +
            KEY_STATUS + " integer not null, " +
            KEY_CREATION_TIME + " integer not null, " +
            KEY_STATUS_TIME + " integer not null, " +
            KEY_PRIORITY + " integer not null " +
            ");";

    public DbMemoRetrieverVer2(String table, DbRetriever<MemoCategory> catRetriever)
    {
        super(table);
        this.catRetriever = catRetriever;
    }

//    protected Memo convertToMemo(Cursor c, SQLiteDatabase db)
    @Override
    protected Memo convertTo(Cursor c, SQLiteDatabase db)
    {
        Memo memo;
        long dateLong;
        Calendar cal;
        int statusId;
        MemoStatusType status;
        int catId;
        MemoCategory cat;
        List<FieldAndValueOperator> fieldValueOperList;
        fieldValueOperList = new ArrayList<FieldAndValueOperator>();

        memo = new MemoVer2();
        memo.setId(c.getInt(0));
        memo.setMemo(c.getString(1));
        catId = c.getInt(2);
//        fieldValueOperList.add(new FieldAndValueOperator(catRetriever.KEY_ROWID, catId, "="));
        fieldValueOperList.add(new FieldAndValueOperator(catRetriever.getKeyField(), catId, "="));
        cat = catRetriever.get(fieldValueOperList, db);
        memo.setCategory(cat);
        statusId = c.getInt(3);
        status = MemoStatusType.getStatus(statusId);
        memo.setStatus(status);
        dateLong = c.getLong(4);
        cal = new GregorianCalendar();
        cal.setTimeInMillis(dateLong);
        memo.setCreationTimestamp(cal);
        dateLong = c.getLong(5);
        cal = new GregorianCalendar();
        cal.setTimeInMillis(dateLong);
        memo.setStatusTimestamp(cal);
        memo.setPriority(c.getInt(6));
        return memo;
    }

    @Override
    protected String[] getColumns()
    {
        return COLUMNS;
    }

    @Override
    public boolean delete(Memo memo, SQLiteDatabase db)
    {
        List<FieldAndValueOperator> fieldValueOperList;
        fieldValueOperList = new ArrayList<FieldAndValueOperator>();
        fieldValueOperList.add(new FieldAndValueOperator(KEY_ROWID, memo.getId(), "="));
        return delete(fieldValueOperList, db);
    }

//    @Override
//    public Memo get(List<FieldAndValueOperator> fieldValueOperList, SQLiteDatabase db)
//    {
//        Memo memo;
//        Cursor c;
//
//        c = db.query(getTable(), getColumns(), getConditionalString(fieldValueOperList), null, null,
//                null, null);
//        memo = convertTo(c, db);
//        return memo;
//    }
    @Override
    public long insert(Memo memo, SQLiteDatabase db)
    {
        List<FieldAndValue> fieldAndValues;

        fieldAndValues = new ArrayList<FieldAndValue>();
        if (memo.getId() > 0)
        {
            fieldAndValues.add(new FieldAndValue(KEY_ROWID, memo.getId()));
        }
        fieldAndValues.add(new FieldAndValue(KEY_MEMO, memo.getMemo()));
        fieldAndValues.add(new FieldAndValue(KEY_CAT, memo.getCategory().getId()));
        fieldAndValues.add(new FieldAndValue(KEY_STATUS, memo.getStatus().getStatusId()));
        fieldAndValues.add(new FieldAndValue(KEY_CREATION_TIME, memo.getCreationTimestamp().
                getTimeInMillis()));
        fieldAndValues.add(new FieldAndValue(KEY_STATUS_TIME, memo.getStatusTimestamp().
                getTimeInMillis()));
        fieldAndValues.add(new FieldAndValue(KEY_PRIORITY, memo.getPriority()));
        return insert(fieldAndValues, db);
    }

    @Override
    protected String getOrderTypeString(OrderType orderType)
    {
        String order;
        switch (orderType)
        {
            case ALPHA:
                order = KEY_MEMO + " COLLATE NOCASE";
                break;
            case NEWEST:
                order = KEY_CREATION_TIME + " DESC";
                break;
            case OLDEST:
                order = KEY_CREATION_TIME;
                break;
            case PRIORITY:
                order = KEY_PRIORITY + " DESC, " + KEY_MEMO + " COLLATE NOCASE";
                break;
            default:
                order = null;
                break;
        }
        return order;
    }

    @Override
    public boolean update(Memo memo, SQLiteDatabase db)
    {
        List<FieldAndValue> fieldAndValues;
        List<FieldAndValueOperator> fieldValueConditions;

        fieldAndValues = new ArrayList<FieldAndValue>();
        fieldValueConditions = new ArrayList<FieldAndValueOperator>();
        fieldAndValues.add(new FieldAndValue(KEY_MEMO, memo.getMemo()));
        fieldAndValues.add(new FieldAndValue(KEY_CAT, memo.getCategory().getId()));
        fieldAndValues.add(new FieldAndValue(KEY_STATUS, memo.getStatus().getStatusId()));
        fieldAndValues.add(new FieldAndValue(KEY_CREATION_TIME, memo.getCreationTimestamp().
                getTimeInMillis()));
        fieldAndValues.add(new FieldAndValue(KEY_STATUS_TIME, memo.getStatusTimestamp().
                getTimeInMillis()));
        fieldAndValues.add(new FieldAndValue(KEY_PRIORITY, memo.getPriority()));
        fieldValueConditions.add(new FieldAndValueOperator(KEY_ROWID, memo.getId(), "="));
        return update(fieldAndValues, fieldValueConditions, db);
    }

    @Override
    public String createTableSQL()
    {
        return DATABASE_CREATE_MEMOS;
    }

    @Override
    public String[] initializeTableSQL()
    {
        return null;
    }

    @Override
    public int getVersion()
    {
        return DATABASE_VERSION;
    }

    @Override
    public String getKeyField()
    {
        return KEY_ROWID;
    }

    public String[] buildTableRestraintsSQL()
    {
        return null;
    }

    public String[] removeTableRestraintsSQL()
    {
        return null;
    }
//
//    @Override
//    public List<Memo> list(SQLiteDatabase db)
//    {
//        return list(null, null, db);
//    }
//
//    @Override
//    public List<Memo> list(OrderType orderType, SQLiteDatabase db)
//    {
//        return list(null, orderType, db);
//    }
//
//    @Override
//    public List<Memo> list(List<FieldAndValueOperator> fieldValueOperList, SQLiteDatabase db)
//    {
//        return list(fieldValueOperList, null, db);
//    }
//
//    @Override
//    public List<Memo> list(List<FieldAndValueOperator> fieldValueOperList, OrderType orderType,
//            SQLiteDatabase db)
//    {
//        String order;
//        List<Memo> memos;
//        Memo memo;
//        Cursor c;
//        String conditions;
//        memos = new ArrayList<Memo>();
//
//        if (fieldValueOperList == null)
//        {
//            conditions = null;
//        }
//        else if (fieldValueOperList.size() == 0)
//        {
//            conditions = null;
//        }
//        else
//        {
//            conditions = getConditionalString(fieldValueOperList);
//        }
//
//        if (orderType == null)
//        {
//            order = null;
//        }
//        else
//        {
//            switch (orderType)
//            {
//                case ALPHA:
//                    order = KEY_MEMO + " COLLATE NOCASE";
//                    break;
//                case NEWEST:
//                    order = KEY_CREATION_TIME + " DESC";
//                    break;
//                case OLDEST:
//                    order = KEY_CREATION_TIME;
//                    break;
//                case PRIORITY:
//                    order = KEY_PRIORITY;
//                    break;
//                default:
//                    order = null;
//                    break;
//            }
//        }
//
//        c = db.query(getTable(), getColumns(), conditions, null, null, null, order);
//
//        if (c.moveToFirst())
//        {
//            memo = convertToMemo(c, db);
//            memos.add(memo);
//        }
//        while (c.moveToNext())
//        {
//            memo = convertToMemo(c, db);
//            memos.add(memo);
//        }
//        return memos;
//    }
}
